<?php
include 'facebook.php';
include 'appinclude.php';

// set up facebook api
$facebook = new Facebook($GLOBALS["api_key"], $GLOBALS["api_secret"]);
$facebook->require_frame();
$user = $facebook->require_login();

// establish link with mysql database
$link = @mysql_connect($GLOBALS["db_host"], $GLOBALS["db_user"], $GLOBALS["db_pass"]);
@mysql_select_db($GLOBALS["db_name"]);

// check if calendar in db
$sem = $GLOBALS["semester"];
$cal_res = mysql_query("SELECT cal_string from cal_strings WHERE uid=$user AND semester='$sem'");
?>

<!-- common html -->

<fb:header>ScheduleMatch</fb:header>

<fb:tabs>
	<fb:tab-item href='index.php' title='Home' selected='true'/>
    <fb:tab-item href='classmates.php' title='Classmates' selected='false'/>
	<fb:tab-item href='settings.php' title='Settings' selected='false'/>
    <fb:tab-item href='invite.php' title='Invite Friends' selected='false'/>
</fb:tabs>

<script language="JavaScript" type="text/javascript">

function showCalendar(data)
{
    if(data.success == "false")
    {
        document.getElementById('calendarSpace').setInnerFBML('not found');
        document.getElementById('cal_frame').setStyle('height', '0px');
    }
    else
    {
        document.getElementById('cal_frame').setStyle('height', '1050px');
        document.getElementById('cal_frame').setSrc(data.url);
    }
}

function submitForm(varForm, url)
{
    document.getElementById('cal_frame').setStyle('height', '0px');
    var ajax = new Ajax();
    ajax.responseType = Ajax.JSON;
    ajax.requireLogin = true;
    ajax.ondone = function(data)
    {
        showCalendar(data);
    };
    ajax.post(url, varForm.serialize()); 
     
}
</script>


<?php if(mysql_num_rows($cal_res) == 0): ?>

<br />
<div align="center">
<?php if($_GET['invalid'] == 1): ?>
This schedule is invalid, please try again.<br /><br />
<?php else: ?>
Before you can see your friends' schedules, please enter your own, eg. http://scheduleman.org/schedules/iTuModla9Jdk<br /><br />
<?php endif; ?>
<form id="cal_add" method="post" action="addschedule.php">
<input type="text" name = "cal_url" id="cal_url">
<select name="sem" id="sem">
  <option value="S10" selected="selected">Spring 2010</option>
  <option value="F09">Fall 2009</option>
</select>

<input type="submit" value="Submit" class="inputsubmit"/> 
</form>
</div>

<?php else: ?>

<br />

<?php if($_GET['posted'] == 1): ?>
<script type="text/javascript">
var img = "<?php echo ($GLOBALS["full_callback"]); ?>" + "/images/schedulematch.png";
var url = "<?php echo($GLOBALS["full_canvas"]); ?>" + "/";
var data = {"images":[{"src": img, "href":url}]};
var template_id = <?php echo $GLOBALS["feed_bundle"]; ?>;
Facebook.showFeedDialog(template_id, data);
</script>
<?php endif; ?>

<div align="center">
<form id = "cal_name" method = "post">
Select a friend whose schedule you'd like to view.<br /><br />
<fb:friend-selector />
<select name="sem" id="sem">
  <option value="S10" selected="selected">Spring 2010</option>
  <option value="F09">Fall 2009</option>
</select>
<input type="button" value="Submit" class="inputsubmit" onclick="submitForm(document.getElementById('cal_name'), '<?php echo($GLOBALS["full_callback"]);?>/render_calendar.php')"/> 
</form>
<div>
<br />
<div id="calendarSpace"></div>
<fb:iframe src="<?php echo($GLOBALS["full_callback"]);?>/blank.htm" id="cal_frame" frameborder=0 width=100% height=0px />
<?php endif; ?>
<?php mysql_close($link); ?>